package com.tyz.hellocst.adapter;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import java.util.List;

public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragmentList;
    private List<String> titles;


    public void setFragmentList(List<Fragment> fragmentList,List<String> titles) {
        this.fragmentList = fragmentList;
        this.titles = titles;
    }



    public MyFragmentPagerAdapter(@NonNull FragmentManager fm,int behavior) {
        super(fm,behavior);

    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        Fragment fragment=null;
        if (position<fragmentList.size()) {
            fragment=fragmentList.get(position);
        }else {
            fragment=fragmentList.get(0);
        }
        return fragment;
    }

    @Override
    public int getCount() {
        return fragmentList.size();
    }

    //注意！此方法是返回导航栏的title，没有就看不见文字
    public CharSequence getPageTitle(int position) {
        if (titles != null && titles.size() > 0)
            return titles.get(position);
        return null;
    }


}
